<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Administrator_video extends MY_Administrator {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/home
	 *	- or -  
	 * 		http://example.com/index.php/home/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
    function __construct()
    {
        parent::__construct();		
		//Load Model
		$this->load->model('administrator_video_model');
    }

	/*Type Management*/
	public function type_management($module=NULL,$arg=NULL)
	{
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_video_model->type_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;
				
				$this->data['content'] = "administrator/video/type_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);	
				//Check Id exist
				$is_exist = $this->db->from('video_type')
				->where(array('video_type_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	
								
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_video_model->type_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;							

				$this->data['dataResult'] = $this->db->get_where('video_type as t',array('t.video_type_id' => $id),1)->row();
	
				$this->data['content'] = "administrator/video/type_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;			
			case 'delete':
				$this->administrator_video_model->type_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "video_type_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
				
				
				/*Search Condition*/
				$searchCondition = array('t.video_type_id IS NOT NULL');
				$video_type_id = $this->input->get('video_type_id');
				$video_type_name = $this->input->get('video_type_name');
				$update_by = $this->input->get('update_by');
				if(!empty($video_type_id)) $searchCondition[] = "t.video_type_id LIKE '%".$video_type_id."%'";
				if(!empty($video_type_name)) $searchCondition[] = "t.video_type_name LIKE '%".$video_type_name."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('video_type as t')->where(''.implode(' AND ',$searchCondition).'')->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->order_by($orderCondition)->get_where('video_type as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['content'] = "administrator/video/type_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}	
		
	/*Video Management*/
	public function content_management($module=NULL,$arg=NULL)
	{			
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
					
		switch($module):
			case 'add':		
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_video_model->content_management_add();
					redirect($this->session->userdata('returnUrl'));
				endif;

				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";									
				
				$this->data['dataVideoType'] = $this->db->order_by('video_type_name','ASC')->get('video_type')->result();
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataGenre'] = $this->db->order_by('genre_name','ASC')->get('genre')->result();
				
				$this->data['content'] = "administrator/video/content_management_add";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
			break;
			case 'edit':
				$id = intval($arg);	
				//Check Id exist
				$is_exist = $this->db->from('video')
				->where(array('video_id' => $id))
				->count_all_results();
				if(empty($is_exist)) show_error('This id is not information.');	
									
				//POST
				if(isset($_POST) && !empty($_POST)):
					$this->administrator_video_model->content_management_edit($id);
					redirect($this->session->userdata('returnUrl'));			
				endif;
				
				//Load Ckeditor
				$this->load->helper('ckeditor');
				//Load Datetime
				$this->data['js'][] = "jquery-ui-1.9.1.custom.min.js";	
				$this->data['js'][] = "jquery-ui-timepicker-addon.js";	
				$this->data['js'][] = "jquery-ui-sliderAccess.js";	
				$this->data['css'][] = "jquery-ui-1.9.1.custom.min.css";				
				//Load Uploadify
				$this->data['js'][] = "uploadify/jquery.uploadify.v2.1.4.min.js";
				$this->data['css'][] = "uploadify/uploadify.css";
				//Load ZeroClipboard
				$this->data['js'][] = "ZeroClipboard/ZeroClipboard.js";										

				$this->data['dataVideoType'] = $this->db->order_by('video_type_name','ASC')->get('video_type')->result();
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataGenre'] = $this->db->order_by('genre_name','ASC')->get('genre')->result();
							
				$this->data['dataResult'] = $this->db
				->join('video_info as j','j.video_id=t.video_id','INNER')
				->get_where('video as t',array('t.video_id' => $id),1)
				->row();
				
				if($this->data['dataResult']->is_relate_song == '1'):
					$this->data['dataVideoSong'] = $this->db->select('j.song_id,j.song_name')->join('song_info as j','j.song_id=t.song_id','INNER')->get_where('video_song as t',array('t.video_id' => $id))->result();
				else:
					$this->data['dataVideoArtist'] = $this->db->select('j.artist_id,j.artist_name,t.is_default')->join('artist_info as j','j.artist_id=t.artist_id','INNER')->get_where('video_artist as t',array('t.video_id' => $id))->result();
					$this->data['dataVideoAlbum'] = $this->db->select('j.album_id,j.album_name')->join('album_info as j','j.album_id=t.album_id','INNER')->get_where('video_album as t',array('t.video_id' => $id))->result();													
				endif;
	
				$this->data['content'] = "administrator/video/content_management_edit";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);		
			break;
			case 'ajax_delete_media':
				$id = intval($arg);
				$field = $this->input->get('field');
				$this->administrator_video_model->ajax_delete_media($id,$field);
			break;						
			case 'delete':
				$this->administrator_video_model->content_management_delete();	
				redirect($this->session->userdata('returnUrl'));		
			break;												
			default:
				/*Keep Url*/
				$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
					
				/*Order Condition*/
				$orderby = $this->input->get('orderby');
				$sort = $this->input->get('sort');
				$orderCondition = "video_id DESC";
				if(!empty($orderby) && !empty($sort)):
					switch($orderby):
						case 'create_date':
						case 'update_date':
							$orderCondition = "t.".$orderby." ".$sort;
						break;
						default:
							$orderCondition = $orderby." ".$sort;
					endswitch;
				endif;
					
				/*Search Condition*/
				$searchCondition = array('t.video_id IS NOT NULL');
				$video_id = $this->input->get('video_id');
				$video_type_id = $this->input->get('video_type_id');
				$media_type_id = $this->input->get('media_type_id');
				$video_name = addslashes($this->input->get('video_name'));
				$video_desc = addslashes($this->input->get('video_desc'));
				$update_by = addslashes($this->input->get('update_by'));
				if(!empty($video_id)) $searchCondition[] = "t.video_id LIKE '%".$video_id."%'";
				if(!empty($video_type_id)) $searchCondition[] = "t.video_type_id = '".$video_type_id."'";
				if(!empty($media_type_id)) $searchCondition[] = "t.media_type_id = '".$media_type_id."'";
				if(!empty($video_name)) $searchCondition[] = "j.video_name LIKE '%".$video_name."%'";
				if(!empty($video_desc)) $searchCondition[] = "j.video_desc LIKE '%".$video_desc."%'";
				if(!empty($update_by)) $searchCondition[] = "t.update_by LIKE '%".$update_by."%'";
				
			  	/*Pager*/		
				$config['base_url'] = current_url().'?'.query_string(array('per_page'));
				$config['total_rows'] = $this->db->from('video as t')
				->join('video_info as j','j.video_id=t.video_id','INNER')
				->where(''.implode(' AND ',$searchCondition).'')
				->count_all_results();
				$config['per_page'] = 20;
				$config['num_links'] = 10;
				$config['page_query_string'] = TRUE;
			  	$this->pagination->initialize($config);
				$this->data['pager'] = $this->pagination->create_links();
				$this->data['dataResult'] = $this->db->select('t.*,j.video_name,j.video_description')
				->join('video_info as j','j.video_id=t.video_id','LEFT')
				->order_by($orderCondition)
				->get_where('video as t',''.implode(' AND ',$searchCondition).'',$config['per_page'],intval($this->input->get('per_page')))->result();
				
				$this->data['dataMediaType'] = $this->db->order_by('media_type_name','ASC')->get('media_type')->result();
				$this->data['dataVideoType'] = $this->db->order_by('video_type_name','ASC')->get('video_type')->result();
				
				$this->data['content'] = "administrator/video/content_management";
				$this->load->view("templates/".$this->folder_templates."templates", $this->data);
		endswitch;		
	}
	
	/*Video Management*/
	public function video_recommend($module=NULL,$arg=NULL)
	{
		//POST
		if(isset($_POST) && !empty($_POST)):
			$this->administrator_video_model->video_recommend();
			redirect($this->session->userdata('returnUrl'));
		endif;		

		/*Keep Url*/
		$this->session->set_userdata('returnUrl', base_url($this->input->server('REQUEST_URI')));
		
		//Load ColorBox
		$this->data['js'][] = "jquery.colorbox-min.js";	
		$this->data['css'][] = "colorbox.css";
		
		$dataJson = json_decode($this->db->get_where('module_page',array('page' => 'video_recommend'),1)->row()->json);
		if(!empty($dataJson)) foreach($dataJson as $value):
			$this->data['dataResult'][] = $this->db->select('video_id,video_name')
			->get_where('video_info','video_id IN ('.implode(',',$value).')')->result();
		endforeach;
		
		$this->data['content'] = 'administrator/video/video_recommend';
		$this->load->view("templates/".$this->folder_templates."templates", $this->data);	
	}	
}